/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.db2;

import java.sql.Connection;
import java.sql.SQLException;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.db3.DBManager3;

public class DBManager2
{
	private static PooledConnection2	conn;
	public static ConnectionPool2		connectionPool;
	public static DBManager2			inst;
	public static boolean				bUse	= false;

	public void close()
	{
		try
		{
			connectionPool.closeConnectionPool();
		}
		catch(SQLException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
	}

	public static void closeConnectionPoolTimeOut()
	{
		try
		{
			connectionPool.closeConnectionPoolTimeOut();
		}
		catch(SQLException e)
		{
			Com_Fun.debug_in(e,DBManager2.class.toString());
		}
	}

	@SuppressWarnings("static-access")
	public DBManager2()
	{
		if(!java.util.Objects.equals(inst,null))
			return;
		com.zxy.model.T_DB_CONFIG tdc = (com.zxy.model.T_DB_CONFIG) Com_Para.getInstance().getInstance().htDb.get("2");
		String connStr = tdc.getDB_url();
		if(tdc.getDB_DriverClassName().equals("org.sqlite.JDBC"))
		{
			connStr = connStr.replace("@",Com_Para.ApplicationPath
					+ Com_Para.zxyPath);
		}
		connectionPool = new ConnectionPool2(tdc.getDB_DriverClassName(),connStr,tdc.getDB_username(),tdc.getDB_password());
		try
		{
			connectionPool.createPool();
			inst = this;
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
	}

	public static PooledConnection2 getConnection()
	{
		if(java.util.Objects.equals(inst,null))
		{
			new DBManager2();
		}
		try
		{
			conn = connectionPool.getConnection();
		}
		catch(SQLException e)
		{
			Com_Fun.debug_in(e,DBManager2.class.toString());
		}
		return conn;
	}

	public static void returnConnection(Connection conn)
	{
		connectionPool.returnConnection(conn);
	}

	public static void refreshConnections()
	{
		connectionPool.refreshConnections();
	}
}